package be.kuleuven.mume.paginas;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.json.JSONObject;

import be.kuleuven.mume.R;
import be.kuleuven.mume.arrayAdapters.AgendaArrayAdapter;
import be.kuleuven.mume.framework.AgendaItem;
import be.kuleuven.mume.framework.AgendaItem.TypeItem;
import be.kuleuven.mume.framework.Mobilenvi;
import be.kuleuven.mume.hulpklassen.DataFetcher;
import be.kuleuven.mume.hulpklassen.HttpHelper;
import be.kuleuven.mume.hulpklassen.JSONHandler;
import be.kuleuven.mume.hulpklassen.JSONHelper;
import be.kuleuven.mume.hulpklassen.JSONParser.AgendaParser;
import be.kuleuven.mume.listProxys.DeviderProxy;
import be.kuleuven.mume.listProxys.LastCellLoadingProxy;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class AgendaPagina extends Activity implements OnItemClickListener{
	private static final int ADDVRIJETIJD_ID = 0;
	private static final int ACTIVITY_ADDAGENDA = 0;
	public static String FILTER_ID = "FILTER";
	private List<AgendaItem> agendaItems;
	private AgendaArrayAdapter adapter;
	private LastCellLoadingProxy loadingProxy;
	private AgendaItem.TypeItem itemType;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.agenda_pagina);
		setTitle(R.string.agenda);
		
		itemType = (AgendaItem.TypeItem)this.getIntent().getExtras().get(AgendaPagina.FILTER_ID);
		
		loadLegende(R.id.agenda_leg_afspr, TypeItem.Afspraken);
		loadLegende(R.id.agenda_leg_uur, TypeItem.Uurooster);
		loadLegende(R.id.agenda_leg_deat, TypeItem.Deathline);
		loadLegende(R.id.agenda_leg_vrij, TypeItem.VrijeTijd);
		
		if(itemType == AgendaItem.TypeItem.Updates)
			tryLoadingData(true);
		else
			tryLoadingData(false);
	}

	private void loadLegende(int id, TypeItem type) {
		TextView v = (TextView)findViewById(id);
		Mobilenvi.getSingelton().setBackgroundColor(v, type);
	}

	private void tryLoadingData(boolean asUpdate) {
		if(agendaItems == null) {
			agendaItems = new ArrayList<AgendaItem>();
			adapter = new AgendaArrayAdapter(this, agendaItems);
			DeviderProxy deviderAdapter = new DeviderProxy(this, adapter);
			ListView list = (ListView)this.findViewById(R.id.agendaItems);
			loadingProxy = new LastCellLoadingProxy(deviderAdapter);
			loadingProxy.noDataString_ID = R.string.geen_agenda_items;
			list.setAdapter(loadingProxy);
			
			new DataFetcher<AgendaItem>(
					new AgendaParser(asUpdate), AgendaItem.getUrl(itemType), list, agendaItems, this) {

				@Override
				public void DataReceived() {
					loadingProxy.setIsLoading(this.hasMoreData());
					Collections.sort(agendaItems);
					adapter.notifyDataSetChanged();
				}
			};
			
			list.setOnItemClickListener(this);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		if(itemType != AgendaItem.TypeItem.Updates)
			menu.add(0, ADDVRIJETIJD_ID,0, R.string.nieuw_vrijetijd);
		return true;
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch(item.getItemId()) {
		case ADDVRIJETIJD_ID:
			Intent i = new Intent(AgendaPagina.this, AgendaEditPagina.class);
			i.putExtra(AgendaItem.TYPE_ID, AgendaItem.TypeItem.VrijeTijd);
			startActivityForResult(i, ACTIVITY_ADDAGENDA);
			return true;
		}

		return super.onMenuItemSelected(featureId, item);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
		super.onActivityResult(requestCode, resultCode, intent);
		
		if(intent == null)
			return;

		Bundle extras = intent.getExtras();

		AgendaItem item;
		switch(requestCode) {
		case ADDVRIJETIJD_ID:
			item = (AgendaItem)extras.get(AgendaItem.AGENDA_ID);
			postVrijeTijd(item,AgendaItem.ADD_URL);
			break;
		}
	}
		
		private void postVrijeTijd(AgendaItem agendaItem, String url) {

			JSONObject obj = JSONHelper.createVrijeTijd(agendaItem);
			
			JSONHandler h = new JSONHandler() {
				
				@Override
				public void handelJSON(JSONObject obj) {
					if(obj.has("error"))
						Toast.makeText(AgendaPagina.this, R.string.serverError, 10).show();
				}
			};
			
			HttpHelper.doPostInThread(url, obj, this.getBaseContext(), h);

			if(!agendaItems.contains(agendaItem)) {
				agendaItems.add(agendaItem);
				Collections.sort(agendaItems);
			}
			adapter.notifyDataSetChanged();

		}

		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			Object o = loadingProxy.getItem(position);
			if (o instanceof AgendaItem) {
				AgendaItem item = (AgendaItem) o;
				
				if(!item.getVakId().equals(""))
				{
					Intent intent = new Intent(this, VakTabBar.class);
					intent.putExtra(VakTabBar.VAK_ID, item.getVakId());
					startActivity(intent);
					return;
				}
				
				if(item.getTypeItem() == TypeItem.VrijeTijd) {
					Intent intent = new Intent(this, AgendaEditPagina.class);
					intent.putExtra(AgendaEditPagina.AGENDA_ITEM, item);
					startActivity(intent);
					return;
				}
				
				Toast.makeText(this, R.string.geen_extra_info, 10).show();
			}
		}
}
